package com.hy.appium2;

import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.AndroidDriver;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;

public class AppiumDemo5Toast {

    static AndroidDriver driver;


    @BeforeMethod
    public void SetUp() throws MalformedURLException {
        //1.需要创建配置对象   desiredcapabilities
        DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
        //将需要启动的软件的一些配置添加进来
        //1.添加需要测试 的主机的路径
        desiredCapabilities.setCapability("deviceName", "127.0.0.1:62001");
        desiredCapabilities.setCapability("platformName", "Android");
        desiredCapabilities.setCapability("appPackage", "com.handsgo.jiakao.android");
        //默认启动不初始化软件
        desiredCapabilities.setCapability("noReset", "true");
        desiredCapabilities.setCapability("appActivity", "com.handsgo.jiakao.android.splash.Login");
        //创建驱动
//        AndroidDriver<WebElement> Driver = new AndroidDriver<WebElement>(url, desiredCapabilities);
        driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), desiredCapabilities);
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

    }

    @Test
    public void test() throws InterruptedException {

        String AssertDialog = "com.handsgo.jiakao.android:id/closeIv";
        if (byElement(By.id(AssertDialog)) == true) {
            driver.findElementById(AssertDialog).click();
        }
        else {
            System.out.println("n");
        }
        //  我的
        driver.findElementByAndroidUIAutomator("new UiSelector().text(\"我的\")").click();
        //  马上登陆
        driver.findElementByAndroidUIAutomator("new UiSelector().text(\"马上登录\")").click();
        //手机号与密码登录
        driver.findElementById("com.handsgo.jiakao.android:id/login_by_account").click();
        //用户名 com.handsgo.jiakao.android:id/username
        driver.findElementById("com.handsgo.jiakao.android:id/username").sendKeys("1111");
        //密码 com.handsgo.jiakao.android:id/password
        driver.findElementById("com.handsgo.jiakao.android:id/password").sendKeys("1111");
        //隐私
        //com.handsgo.jiakao.android:id/check_box
        driver.findElementById("com.handsgo.jiakao.android:id/check_box").click();
        //点击登录
        //com.handsgo.jiakao.android:id/btn_ok
        driver.findElementById("com.handsgo.jiakao.android:id/btn_ok").click();
        WebElement elementByXPath = driver.findElementByXPath("//*[contains(@text,'手机号')]");
        String text = elementByXPath.getText();
        System.out.println(text);

        //强制等待
//        Thread.sleep(20000);







    }

    @AfterMethod
    public void TestDown() {
//        Driver.quit();
    }

    public static boolean byElement(By btId) {
        try {
            driver.findElement(btId);
            return true;
        } catch (Exception e) {
            return false;
        }
    }
}
